package Servlet;

import Bean.RegistResult;
import Bean.RespContent;
import Server.Userservlet;
import Server.Userservletimpl;
import com.google.gson.Gson;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

@WebServlet(name = "RegistServlet", urlPatterns = "/RegistServlet")
public class RegistServlet extends BaseServlet {
    protected void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        String name = request.getParameter("username");
        String psd = request.getParameter("password");
        Userservlet uc=new Userservletimpl();
         RespContent<RegistResult> respContent=new  RespContent<>();
          Gson gson=new Gson();
          Boolean s = uc.insertUserservletimpl(name,psd);
            if (s){
                respContent.setCode(20);
                respContent.setMsg("恭喜注册成功");
//            response.sendRedirect("/login/login/succ.jsp");
            }else {
                respContent.setCode(30);
                respContent.setMsg("用户名或密码不正确，请重新输入!");

//            response.sendRedirect("/login/login/loginFail.html");
            }
//        String toJson = gson.toJson(respContent);
//        response.getWriter().print(respContent);
        response.getWriter().print(gson.toJson(respContent));
    }
}
